package com.webservice.impl;


import com.dao.BiddingDao;
import com.dao.EquipmentDao;
import com.dao.Maintenance_orderDao;
import com.dao.UserDao;
import com.domain.*;
import com.webservice.OrderWebservice;
import com.tool.Until;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebService(targetNamespace = "http://39.98.59.6/mmms_test/ws/order")
public class OrderWebserviceImpl implements OrderWebservice {

    @Autowired
    private Maintenance_orderDao maintenance_orderDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private BiddingDao biddingDao;

    @Autowired
    private EquipmentDao equipmentDao;

    @Autowired
    private Until until;

    @Resource
    private RuntimeService runtimeService;


    //维修单详细信息
    private Order_details findDetails(Maintenance_order maintenance_order){
        Order_details order_details = new Order_details();
        if (maintenance_order!=null){
            maintenance_order = until.setOrder(maintenance_order);
            order_details.setMaintenance_order(maintenance_order);
            List<Maintenance_process> processList = maintenance_orderDao.findOrderProcess(maintenance_order.getId());
            if (processList!=null){
                order_details.setMaintenanceProcessList(processList);
            }
            else {
                return order_details;
            }
            if (maintenance_order.getUnit_id()!=null && Until.findById(maintenance_order.getUnit_id()) != null){
                List<Expense_details> expense_detailsList = maintenance_orderDao.findExpenseDetails(maintenance_order.getId(), maintenance_order.getUnit_id());
                if (expense_detailsList!=null){
                    order_details.setExpense_detailsList(expense_detailsList);
                }
                else {
                    return order_details;
                }
            }

            Evaluation evaluation = maintenance_orderDao.findEvaluation(maintenance_order.getId());
            if (evaluation!=null){
                order_details.setEvaluation(evaluation);
            }
            else {
                return order_details;
            }
            List<Additional_evaluation> additional_evaluationList = maintenance_orderDao.findAdditionalEvaluation(maintenance_order.getId());
            if (additional_evaluationList!=null){
                order_details.setAdditional_evaluationList(additional_evaluationList);
            }
            else {
                return order_details;
            }
        }
        return order_details;
    }


    @WebResult(name = "Maintenance_order")
    @Override
    public Maintenance_order findOne(@WebParam(name = "id") Integer id) {
        Maintenance_order maintenance_order = maintenance_orderDao.findOneById(id);
        maintenance_order = until.setOrder(maintenance_order);
        return maintenance_order;
    }


    /**
     * 新增方法
     * 查找一个维修单所有的维修要求
     * @param id
     * @return
     */
    @WebResult(name = "Maintenance_requestList")
    @Override
    public List<Maintenance_request> findAllRequest(@WebParam(name = "id")Integer id){
        List<Maintenance_request> maintenance_requestList = maintenance_orderDao.findRequestByOrderId(id);
        List<Maintenance_request> requests = new ArrayList<>();
        for (Maintenance_request request:maintenance_requestList){
            User user = Until.findById(request.getUser_id());
            request.setUser_name(user.getName());
            if (Until.findById(request.getUser_id()).getRole_id() == 2){
                request.setRole_id(2);
            }
            if (Until.findById(request.getUser_id()).getRole_id() == 3){
                request.setRole_id(3);
            }
            if (Until.findById(request.getUser_id()).getRole_id() == 4){
                request.setRole_id(4);
//                if (Until.findById(request.getUser_id()).getRank_id() == 3){
//                    request.setRank_id(3);
//                }
//                if (Until.findById(request.getUser_id()).getRank_id() == 4){
//                    request.setRank_id(4);
//                }
//                if (Until.findById(request.getUser_id()).getRank_id() == 5){
//                    request.setRank_id(5);
//                }
            }
            requests.add(request);
        }
        return requests;
    }


    /**
     * 新增方法
     * 查找一个维修单所有的维修清单
     * @param id
     * @return
     */
    @WebResult(name = "ExpenseDetailsList")
    @Override
    public List<Expense_details> findxpenseDetailsByOrder(@WebParam(name = "id")Integer id){
        if (id !=null && maintenance_orderDao.findOneById(id) != null){
            Maintenance_order order = maintenance_orderDao.findOneById(id);
            if (Until.findById(order.getUnit_id()) != null){
                List<Expense_details> expense_detailsList = maintenance_orderDao.findExpenseDetails(id,order.getUnit_id());
                if (expense_detailsList!=null){
                    return expense_detailsList;
                }
                else
                    return new ArrayList<Expense_details>();
            }
            else {
                return new ArrayList<Expense_details>();
            }

        }
        else {
            return new ArrayList<Expense_details>();
        }
    }





    /**
     * 新增方法
     * 查询一个审核员所有待审批的维修单
     * @param approver_id
     * @return
     */
    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findOrderReview(@WebParam(name = "approver_id") Integer approver_id){
        List<Maintenance_order> maintenance_orderList = maintenance_orderDao.findOrderByUser(approver_id);
        ArrayList<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        if (maintenance_orderList!=null){
            for (Maintenance_order maintenance_order:maintenance_orderList){
                maintenance_order = until.setOrder(maintenance_order);
                if (!maintenance_orderList1.contains(maintenance_order)){
                    if (maintenance_order.getOrder_state() != 4 &&
                            maintenance_order.getOrder_state() != 7 &&
                            maintenance_order.getOrder_state() != 12){
                        continue;
                    }
                    Date now = new Date(System.currentTimeMillis());
                    long day = (now.getTime()-maintenance_order.getCreateTime().getTime())/(24*60*60*1000);
                    if (day <= maintenance_order.getOrderPeriodOfValidity()){
                        maintenance_orderList1.add(maintenance_order);
                    }
                }
            }
        }
        return maintenance_orderList1;
    }

    /**
     * 查找一个维修单所有的流程
     * @param id
     * @return
     */
    @WebResult(name = "Maintenance_processList")
    @Override
    public List<Maintenance_process> findProcess(@WebParam(name = "id")Integer id){
        List<Maintenance_process> processList = maintenance_orderDao.findOrderProcess(id);
        return processList;
    }


    /**
     * 查找一个维修单的评价
     * @param id 维修单id
     * @return
     */
    @WebResult(name = "Evaluation")
    public Evaluation findEvaluation(@WebParam(name = "id")Integer id){
        return maintenance_orderDao.findEvaluation(id);
    }


    /**
     * 查找一个维修单的追评
     * @param id
     * @return
     */
    @WebResult(name = "Additional_evaluationList")
    public List<Additional_evaluation> findAdditionalEvaluation(@WebParam(name = "id")Integer id){
        return maintenance_orderDao.findAdditionalEvaluation(id);
    }


    //查找一个维修单详情
    @WebResult(name = "Order_details")
    @Override
    public Order_details findOrderDetails(@WebParam(name = "id") Integer id) {
        Maintenance_order maintenance_order = maintenance_orderDao.findOneById(id);
        Order_details order_details = findDetails(maintenance_order);
        return order_details;
    }

    //按公司维修类型接收维修单数据
    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findByState(@WebParam(name = "order_state_id") Integer order_state_id,
                                               @WebParam(name = "company_id") Integer company_id,
                                               @WebParam(name = "flag") Integer flag) {
        User company = until.findById(company_id);
        List<Maintenance_order> maintenance_orderList = maintenance_orderDao.findByState(order_state_id);
        List<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        for (Maintenance_order maintenance_order : maintenance_orderList){
            if (company.getType_id() == 3 || company.getType_id().equals(maintenance_order.getDepartment_id())){
                Task task = Until.findTaskById(maintenance_order.getId());
                List<User> users = (List<User>) runtimeService.getVariable(task.getExecutionId(),"unit");
                User unit = Until.findById(company_id);
                if ((users != null && users.size() > 0) && !users.contains(unit)){
                    continue;
                }
                maintenance_order = until.setOrder(maintenance_order);
                Bidding bidding = biddingDao.findBiddingByCompany(maintenance_order.getId(), company_id);
                if (flag == 1 && bidding == null){
                    List<Maintenance_process> processList = maintenance_orderDao.findOrderProcess(maintenance_order.getId());
                    if (processList!=null){
                        Date bidingTime = new Date();
                        Date now = new Date(System.currentTimeMillis());
                        for (Maintenance_process process:processList){
                            if (process.getTask_id() == 2){
                                bidingTime = process.getFinished_time();
                            }
                        }
                        long day = (now.getTime()-bidingTime.getTime())/(24*60*60*1000);
                        if (maintenance_order.getBiddingPeriodOfValidity() == null || day <= maintenance_order.getBiddingPeriodOfValidity()){
                            maintenance_orderList1.add(maintenance_order);
                        }
                    }
                }
                else if (flag == 2 && bidding != null){
                    maintenance_orderList1.add(maintenance_order);
                }
            }


        }
        return maintenance_orderList1;
    }

    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findHistoryByUser(@WebParam(name = "user_id")Integer user_id,
                                                     @WebParam(name = "time") String time) {
        List<Maintenance_order> maintenance_orderList = maintenance_orderDao.findHistoryByUser(time, user_id);
        ArrayList<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        for (Maintenance_order maintenance_order:maintenance_orderList){
            maintenance_order = until.setOrder(maintenance_order);
            if (!maintenance_orderList1.contains(maintenance_order)){
                maintenance_orderList1.add(maintenance_order);
            }
        }
        return maintenance_orderList1;
    }

    @Override
    @WebResult(name = "Maintenance_orderList")
    public List<Maintenance_order> findHistoryByUserYear(@WebParam(name = "user_id")Integer user_id,
                                                         @WebParam(name = "year") Integer year) {
        List<Maintenance_order> maintenance_orderList = maintenance_orderDao.findHistoryByUserYear(year, user_id);
        ArrayList<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        for (Maintenance_order maintenance_order:maintenance_orderList){
            maintenance_order = until.setOrder(maintenance_order);
            if (!maintenance_orderList1.contains(maintenance_order)){
                maintenance_orderList1.add(maintenance_order);
            }
        }
        return maintenance_orderList1;
    }

    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findOrderByUser(@WebParam(name = "user_id")Integer user_id) {
        List<Maintenance_order> maintenance_orderList = maintenance_orderDao.findOrderByUser(user_id);
        ArrayList<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        if (maintenance_orderList!=null){
            for (Maintenance_order maintenance_order:maintenance_orderList){
                maintenance_order = until.setOrder(maintenance_order);
                if (!maintenance_orderList1.contains(maintenance_order)){
                    if (maintenance_order.getOrder_state() != 15){
                        Date now = new Date(System.currentTimeMillis());
                        long day = (now.getTime()-maintenance_order.getCreateTime().getTime())/(24*60*60*1000);
                        if (day <= maintenance_order.getOrderPeriodOfValidity()){
                            maintenance_orderList1.add(maintenance_order);
                        }
                    }

                }
            }
        }
        return maintenance_orderList1;
    }

    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findOrderByUnit(@WebParam(name = "user_id")Integer user_id) {
        List<Maintenance_order> maintenance_orderList = maintenance_orderDao.findOrderByUnit(user_id);
        ArrayList<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        if (maintenance_orderList!=null){
            for (Maintenance_order maintenance_order:maintenance_orderList){
                maintenance_order = until.setOrder(maintenance_order);
                if (maintenance_order.getOrder_state() != 15){
                    Date now = new Date(System.currentTimeMillis());
                    long day = (now.getTime()-maintenance_order.getCreateTime().getTime())/(24*60*60*1000);
                    if (day <= maintenance_order.getOrderPeriodOfValidity()){
                        maintenance_orderList1.add(maintenance_order);
                    }
                }
            }
        }
        return maintenance_orderList1;
    }

    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findHOrderByEquipment(@WebParam(name = "equipment_id")Integer equipment_id) {
        List<Maintenance_order> maintenance_orderList = maintenance_orderDao.findHOrderByEquipment(equipment_id);
        List<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        for (Maintenance_order maintenance_order:maintenance_orderList){
            maintenance_order = until.setOrder(maintenance_order);
            maintenance_orderList1.add(maintenance_order);
        }
        return maintenance_orderList1;
    }

    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findOrderByEquipment(@WebParam(name = "equipment_id")Integer equipment_id) {
        List<Maintenance_order> maintenance_orderList = maintenance_orderDao.findHOrderByEquipment(equipment_id);
        List<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        for (Maintenance_order maintenance_order:maintenance_orderList){
            maintenance_order = until.setOrder(maintenance_order);
            maintenance_orderList1.add(maintenance_order);
        }
        return maintenance_orderList1;
    }

    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findAll(@WebParam(name = "time") String time,
                                           @WebParam(name = "department_id") Integer department_id) {
        List<Maintenance_order>maintenance_orderList = maintenance_orderDao.findAll(time, department_id);
        List<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        for (Maintenance_order maintenance_order:maintenance_orderList){
            maintenance_order = until.setOrder(maintenance_order);
            maintenance_orderList1.add(maintenance_order);
        }
        return maintenance_orderList1;
    }

    @Override
    @WebResult(name = "Maintenance_orderList")
    public List<Maintenance_order> findAllYear(@WebParam(name = "year") Integer year,
                                               @WebParam(name = "department_id") Integer department_id) {
        List<Maintenance_order>maintenance_orderList = maintenance_orderDao.findAllYear(year, department_id);
        List<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        for (Maintenance_order maintenance_order:maintenance_orderList){
            maintenance_order = until.setOrder(maintenance_order);
            maintenance_orderList1.add(maintenance_order);
        }
        return maintenance_orderList1;
    }

    /**
     * 根据年查询所有维修单
     * @param year
     * @return
     */
    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findAllOrderByYear(@WebParam(name = "year") Integer year){
        List<Maintenance_order>maintenance_orderList = maintenance_orderDao.findAllOrderByYear(year);
        List<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        for (Maintenance_order maintenance_order:maintenance_orderList){
            maintenance_order = until.setOrder(maintenance_order);
            maintenance_orderList1.add(maintenance_order);
        }
        return maintenance_orderList1;
    }



    /**
     * 按年月显示所有维修单
     * @return
     */
    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findAllOrderByTime(@WebParam(name = "time") String time){
        List<Maintenance_order>maintenance_orderList = maintenance_orderDao.findAllOrderByTime(time);
        List<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        for (Maintenance_order maintenance_order:maintenance_orderList){
            maintenance_order = until.setOrder(maintenance_order);
            maintenance_orderList1.add(maintenance_order);
        }
        return maintenance_orderList1;
    }

    /**
     * 按年月显示所有未完成维修单
     * @return
     */
    @WebResult(name = "Maintenance_orderList")
    public List<Maintenance_order> findAllOrderByTimeUnfinished(@WebParam(name = "time") String time){

        List<Maintenance_order>maintenance_orderList = maintenance_orderDao.findAllOrderByTimeUnFinished(time);
        List<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        for (Maintenance_order maintenance_order:maintenance_orderList){
            maintenance_order = until.setOrder(maintenance_order);
            maintenance_orderList1.add(maintenance_order);
        }
        return maintenance_orderList1;
    }

    /**
     * 按年月显示所有已完成维修单
     * @return
     */
    @WebResult(name = "Maintenance_orderList")
    public List<Maintenance_order> findAllOrderByTimeFinished(@WebParam(name = "time") String time){

        List<Maintenance_order>maintenance_orderList = maintenance_orderDao.findAllOrderByTimeFinished(time);
        List<Maintenance_order> maintenance_orderList1 = new ArrayList<Maintenance_order>();
        for (Maintenance_order maintenance_order:maintenance_orderList){
            maintenance_order = until.setOrder(maintenance_order);
            maintenance_orderList1.add(maintenance_order);
        }
        return maintenance_orderList1;
    }


    /**
     * 根据单位查询所属维修单
     * @param insiderUnit_id
     * @return
     */
    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findAllOrderByInsiderUnit(@WebParam(name = "insiderUnit_id")Integer insiderUnit_id){
        List<Maintenance_order> orders = maintenance_orderDao.findAllOrder();
        List<Maintenance_order> maintenance_orders = new ArrayList<>();
        User user = new User();
        if (orders!=null){
            for (Maintenance_order order:orders){
                List<Maintenance_process> processList = maintenance_orderDao.findOrderProcess(order.getId());
                if (processList!=null){
                    for (Maintenance_process process:processList){
                        if (process.getTask_id() == 1){
                            user = until.findById(process.getTransactor_id());
                            break;
                        }
                    }
                    if (user.getInsiderUnit_id().equals(insiderUnit_id)) {
                        order = until.setOrder(order);
                        maintenance_orders.add(order);
                    }
                }
            }
        }
        return maintenance_orders;

    }

    /**
     * 根据维修单名称模糊查询
     * @param order_name
     * @return
     */
    @WebResult(name = "Maintenance_orderList")
    @Override
    public List<Maintenance_order> findOrderByName(@WebParam(name = "order_name") String order_name){
        List<Maintenance_order> orders = maintenance_orderDao.findOrderByName(order_name);
        List<Maintenance_order> maintenance_orders = new ArrayList<>();
        if (orders!=null){
            for (Maintenance_order order:orders){
                order = Until.setOrder(order);
                maintenance_orders.add(order);
            }
        }
        return maintenance_orders;
    }




    /**
     * 根据维修单名称和部门模糊查询
     * @param order_name
     * @return
     */
    @WebResult(name = "Maintenance_orderList")
    public List<Maintenance_order> findDepartmentOrderByName(@WebParam(name = "department_id") Integer department_id,
                                                             @WebParam(name = "order_name") String order_name){
        List<Maintenance_order> orders = maintenance_orderDao.findDepartmentOrderByName(department_id, order_name);
        List<Maintenance_order> maintenance_orders = new ArrayList<>();
        if (orders!=null){
            for (Maintenance_order order:orders){
                order = Until.setOrder(order);
                maintenance_orders.add(order);
            }
        }
        return maintenance_orders;
    }


    /**
     * 根据单位和维修单名称模糊查询
     * @param insiderUnit_id
     * @param order_name
     * @return
     */
    @WebResult(name = "Maintenance_orderList")
    public List<Maintenance_order> findInsiderUnitOrderByName(@WebParam(name = "insiderUnit_id") Integer insiderUnit_id,
                                                              @WebParam(name = "order_name") String order_name){
        List<Maintenance_order> orders = maintenance_orderDao.findInsiderUnitOrderByName(insiderUnit_id, order_name);
        List<Maintenance_order> maintenance_orders = new ArrayList<>();
        if (orders!=null){
            for (Maintenance_order order:orders){
                order = Until.setOrder(order);
                maintenance_orders.add(order);
            }
        }
        return maintenance_orders;
    }

    @Override
    public List<Maintenance_order> findOrderByType(@WebParam(name = "maintenance_type") Integer maintenance_type) {
        List<Maintenance_order> maintenance_orderList = maintenance_orderDao.findOrderByType(maintenance_type);
        List<Maintenance_order> maintenance_orders = new ArrayList<>();
        if (maintenance_orderList!=null){
            for (Maintenance_order order:maintenance_orderList){
                order = until.setOrder(order);
                maintenance_orders.add(order);
            }
        }
        return maintenance_orders;
    }


    /**
     * 查询所有正在进行的维修单
     * @return
     */
    @WebResult(name = "Maintenance_orderList")
    public List<Maintenance_order> findAllUnfinishedOrder(){
        List<Maintenance_order> maintenance_orderList = maintenance_orderDao.findAllUnfinishedOrder();
        List<Maintenance_order> maintenance_orders = new ArrayList<>();
        if (maintenance_orderList!=null){
            for (Maintenance_order order:maintenance_orderList){
                order = until.setOrder(order);
                maintenance_orders.add(order);
            }
        }
        return maintenance_orders;
    }
}
